<title>管理员</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a><cite>用户管理</cite></a>
        <a><cite>管理员</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-useradmin-formlist">
            <div class="layui-form-item">
                <input type="hidden" name="type" value="1">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="account" placeholder="登陆账号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <select name="roleId" id="role-search">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <select name="check">
                            <option value="">请选择</option>
                            <option value="0">禁用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        搜索
                    </button>
                    <button class="layui-btn layuiadmin-btn-admin" data-type="add">
                        添加
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.check == 1){ }}
                <button class="layui-btn layui-btn-xs">启用</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">禁用</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">
                    <i class="layui-icon layui-icon-edit"></i>编辑</a>

                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="initPass">
                    <i class="layui-icon layui-icon-password"></i>初始化密码</a>

                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail">
                    <i class="layui-icon layui-icon-about"></i>详情</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table', 'setter', 'form'], function () {
        var $ = layui.$
            , admin = layui.admin
            , view = layui.view
            , table = layui.table
            , setter = layui.setter
            , form = layui.form;

        var apiUrl = setter.apiUrl;


        //管理员管理
        table.render({
            elem: '#LAY-user-back-manage'
            , url: apiUrl + '/mapi/admin/list'//'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , headers: {'token': layui.data(setter.tableName).token, 'Content-Type': 'application/json;charset=UTF-8'}
            , request: {
                pageName: 'pageNo'
                , limitName: 'pageSize'
            }
            , where: {type: 1}
            , response: {
                statusCode: 0, //规定数据状态的字段名称，默认：code
            }
            , parseData: function (res) {
                if (res.code === 0) {
                    //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                } else {
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                    };
                }
            }
            , cols: [[
                // {type: 'checkbox', fixed: 'left'}
                {field: 'id', title: '用户id', minWidth: 80}
                , {
                    field: 'createdAt',
                    title: '创建时间',
                    templet: '<div>{{layui.util.toDateString(d.createdAt, "yyyy-MM-dd HH:mm:ss")}}</div>'
                }
                , {field: 'account', title: '登录名'}
                , {field: 'mobile', title: '手机'}
                , {field: 'role', title: '角色', templet: '<div>{{d.role?d.role.name:""}}</div>'}
                , {field: 'check', title: '状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
                , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
            // , done: function () {
            // $("[data-field='id']").css('display', 'none');
            // }
            , text: {
                none: '未检索到数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });

        //监听工具条
        table.on('tool(LAY-user-back-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                admin.popup({
                    title: '编辑管理员'
                    , area: ['420px', '450px']
                    , id: 'LAY-popup-user-add'
                    , success: function (layero, index) {
                        view(this.id).render('user/admin/edit', data).done(function () {
                            var uid = data.id;
                            admin.req({
                                url: apiUrl + '/mapi/permission/r/list'
                                , method: 'post'
                                , data: JSON.stringify({})
                                , done: function (response) {
                                    if (response.code === 0) {
                                        var list = response.data.list;
                                        var html = "";
                                        for (var i = 0; i < list.length; i++) {
                                            html += "<option value=" + list[i]["id"] + ">" + list[i]["name"] + "</option>";
                                        }
                                        $("#role").append(html);

                                        $("#role").val(data.roleId);
                                        form.render('select');
                                    }
                                }
                            });

                            form.render(null, 'layuiadmin-form-admin');

                            //监听提交
                            form.on('submit(LAY-user-back-submit)', function (data) {
                                if (data.field.check === 'on') {
                                    data.field.check = 1;
                                } else {
                                    data.field.check = 0;
                                }
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                admin.req({
                                    url: apiUrl + '/mapi/admin/update/' + uid //实际使用请改成服务端真实接口
                                    , method: 'put'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('修改成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-user-back-manage'); //重载表格
                                                layer.close(index); //执行关闭
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            } else if (obj.event === 'detail') {
                admin.popup({
                    title: '详细信息'
                    , area: ['100%', '100%']
                    , id: 'LAY-popup-admin-detail'
                    , success: function (layero, index) {
                        view(this.id).render('user/admin/detail', data);
                    }
                });
            } else if (obj.event === 'initPass') {
                top.layer.confirm('确定要初始化' + '"' + data.account + '"密码吗？', {
                        skin: 'layui-layer-admin'
                    }, function (index) {
                        top.layer.close(index);
                        admin.req({
                            url: apiUrl + '/mapi/admin/init_pass/' + data.id
                            , method: 'get'
                            , done: function (response) {
                                if (response.code === 0) {
                                    layer.msg('初始化成功', {
                                        offset: '15px', icon: 1, time: 1000
                                    });
                                } else {
                                    layer.msg(response.msg, {
                                        offset: '15px', icon: 2
                                    });
                                }
                            }
                        });
                    }
                );
            }
        });

        admin.req({
            url: apiUrl + '/mapi/permission/r/list' //实际使用请改成服务端真实接口
            , method: 'post'
            , data: JSON.stringify({})
            , done: function (response) {
                if (response.code === 0) {
                    var list = response.data.list;
                    var html = "";
                    for (var i = 0; i < list.length; i++) {
                        html += "<option value=" + list[i]["id"] + ">" + list[i]["name"] + "</option>";
                    }
                    $("#role-search").append(html);
                    form.render('select');
                }
            }
        });

        form.render(null, 'layadmin-useradmin-formlist');

        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;

            //执行重载
            table.reload('LAY-user-back-manage', {
                where: field
            });
        });

        //事件
        var active = {
            add: function () {
                admin.popup({
                    title: '添加管理员'
                    , area: ['420px', '430px']
                    , id: 'LAY-popup-useradmin-add'
                    , success: function (layero, index) {
                        view(this.id).render('user/admin/add').done(function () {
                            form.render(null, 'layuiadmin-form-admin');
                            //监听提交
                            form.on('submit(LAY-user-add-submit)', function (data) {
                                if (data.field.check === 'on') {
                                    data.field.check = 1;
                                } else {
                                    data.field.check = 0;
                                }
                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                admin.req({
                                    url: apiUrl + '/mapi/admin/add' //实际使用请改成服务端真实接口
                                    , method: 'post'
                                    , data: JSON.stringify(data.field)
                                    , done: function (response) {
                                        if (response.code === 0) {
                                            layer.msg('添加成功', {
                                                offset: '15px', icon: 1, time: 1000
                                            }, function () {
                                                layui.table.reload('LAY-user-back-manage'); //重载表格
                                                layer.close(index); //执行关闭
                                            });
                                        } else {
                                            layer.msg(response.msg, {
                                                offset: '15px', icon: 2
                                            });
                                        }
                                    }
                                });
                            });
                        });
                    }
                });
            }
        }
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>